package com.lc.ibps.platform.rest.org;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.org.service.IPartyGroupService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户组服务 控制器，第三方系统调用。
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：eddy
 * 邮箱：1546077710@qq.com
 * 日期：2016年9月22日-下午2:47:07
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */

@RequestMapping(value = "/api/webapi/groupService")
@Api(value = "/groupService", tags = "用户管理-用户组服务")
@RestController
public class GroupController extends BaseApiController {

	private IPartyGroupService groupService;

	public GroupController() {
		groupService = AppUtil.getBean(IPartyGroupService.class);
	}

	@ApiOperation(value = "根据ID获取用户组", notes = "根据ID获取用户组")
	@RequestMapping(value = "/loadById", method = RequestMethod.GET)
	public APIResult<String> loadById(
			@ApiParam(value = "id", required = true) @RequestParam(name = "id", required = true) String id) {
		APIResult<String> result = new APIResult<>();
		try {
			String data = groupService.getByIdJson(id);
			result.setData(data);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户组失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据别名获取用户组", notes = "根据别名获取用户组")
	@RequestMapping(value = "/loadByAlias", method = RequestMethod.GET)
	public APIResult<String> loadByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<String> result = new APIResult<>();
		try {
			String group = groupService.getByAliasJson(alias);
			result.setData(group);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户组失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取用户组列表", notes = "获取用户组列表")
	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	public APIResult<String> findAll() {

		APIResult<String> result = new APIResult<>();
		try {
			String group = groupService.findAllJson();
			result.setData(group);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户组失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用户账号获取用户组列表", notes = "根据用户账号获取用户组列表")
	@RequestMapping(value = "/findByUserAccount", method = RequestMethod.GET)
	public APIResult<String> findByUserAccount(
			@ApiParam(value = "account", required = true) @RequestParam(name = "account", required = true) String account) {

		APIResult<String> result = new APIResult<>();
		try {
			String group = groupService.findByUserAccountJson(account);
			result.setData(group);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户组失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用户ID获取用户组列表", notes = "根据用户ID获取用户组列表")
	@RequestMapping(value = "/findByUserId", method = RequestMethod.GET)
	public APIResult<String> findByUserId(
			@ApiParam(value = "userId", required = true) @RequestParam(name = "userId", required = true) String userId) {
		APIResult<String> result = new APIResult<>();
		try {
			String group = groupService.findByUserIdJson(userId);
			result.setData(group);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户组失败！", e);
		}
		return result;
	}

}
